<?php
namespace app\admin\controller;

use think\Request;

class Admin extends Common
{


    //管理员首页
    public function index()
    {
        $adminlist = db('admin')->where("id != 1")->paginate(10);
        //$rolelist = db('role')->select();
        $page=$adminlist->render();
        $page = str_replace(
            ["pagination",
            "disabled",
            "active"],
        ["am-pagination tpl-pagination",
        "am-disabled",
        "am-active"],
        $page);
        //用数据对象的形式进行访问
        $adminlist=$adminlist->items();
        //链表查询
        $all_admin_role = db('admin_role')->alias('a')->join('role b','a.role_id= b.role_id')->field('a.*,b.name as role_name')->select();
    //   dump($all_admin_role);
    //   dump($adminlist);die;


        foreach ($all_admin_role as $k1 => $v1) {
            foreach ($adminlist as $k2 => $v2) {
                if($v1['admin_id']==$v2['id']){
                    //$adminlist[$k2]['role']['role_id'][]=$v1['role_id'];
                    $adminlist[$k2]['role'][]=$v1['role_name'];
                }else{
                    $adminlist[$k2]['role'][]='';
                }   
            }            
        }
        //dump($adminlist);die;

        $this->assign('adminlist' , $adminlist);
        $this->assign('page' , $page);
        return view();
    }
    //管理员添加页面
    public function add()
    {
        return view();
    }

    //上传轮播图
    public function upimg(){

        $file = request()->file('src');
        if($file){
            $info = $file->move(ROOT_PATH . 'public' . DS . 'uploads');
            if($info){
                $img_src = '/uploads/'.$info->getSaveName();
                //echo $img_src;
                return ['src'=>$img_src];
                // return ['src'=>$img_src];
            }
        }
        else{
                $this->error($file->getError());
            }
    }
    //新增添加入库
    public function add_admin(){
        $data = input('post.');
        $data['password']=md5($data['password']);
         $validate = validate('Admin1');
         if(!$validate->check($data)){
             $this->error($validate->getError());
         }
        $ret = db('admin')->insert($data);
        if($ret){
            $this->success('添加成功','admin/admin/index');
        }else{
            $this->error('添加失败');
        }
    }
    //管理员角色编辑页面
    public function edit()
    {
        $admin_id=input('param.id');
        $rolelist = db('role')->where("role_id !=1")->select();
        //dump($rolelist);
        $this->assign('rolelist' , $rolelist);
        //$data = input('post.');
        $result = db('admin')->where("id=$admin_id")->find();
        $admin_role = db('admin_role')->where("admin_id=$admin_id")->select();
        $role_list=[];
        foreach($admin_role as $k => $v){
            $role_list[] = $v['role_id'];
        }
        
        //dump($role_list);die;
        $this->assign('result',$result);
        $this->assign('role_list',$role_list);
        return view();
    }

    //改管理员角色
    public function update()
    {
        $admin_id=input('param.admin_id');
        $data=input('post.');
        $role_id=input('post.role_id/a');
        //dump($role_id);die;
        $validate = validate('Admin2');
        if(!$validate->check($data)){
            $this->error($validate->getError());
        }  
        //dump($role_id);die;
        $admin_permission=[];
        for($i=0;$i<count($role_id);$i++){
            $admin_permission[]=['admin_id'=>$admin_id,'role_id'=>$role_id[$i]];
        }
       // dump($admin_permission);die;
         $ret=db('admin_role')->where("admin_id=$admin_id")->delete();
         $ret=db('admin_role')->insertAll($admin_permission);
        if($ret == false){
            $this->error('权限编辑失败');
        }else{
            $this->success('权限编辑成功');
        }
    }

    //删除管理员
    public function delete(){
        $id=$_GET['id'];
        $ret=db('admin')->where("id=$id")->delete();
        if($ret){
            //删除管理员角色
            $ret1 = db('admin_role')->where("admin_id=$id")->select();
            if(empty($ret1)){
                $this->success("删除成功!");
            }else{
                $ret2 = db('admin_role')->where("admin_id=$id")->delete();
                if($ret2){
                    $this->success("删除成功!");
                }else{
                    $this->error("删除失败");
                }
            }
            
        }else{
            $this->error("删除失败");
        }
    }
//    //角色列表
//     public function rolelist(){
//         // echo '111';
//         $role_list = db('role')->paginate(2);
//         $page=$role_list->render();
//         $page = str_replace(
//             ["pagination",
//             "disabled",
//             "active"],
//         ["am-pagination tpl-pagination",
//         "am-disabled",
//         "am-active"],
//         $page);
//         //用数据对象的形式进行访问
//         $role_list=$role_list->items();
//        $this->assign('role_list',$role_list);
//        $this->assign('page',$page);
//         return view();
//     }

//     //角色子权限编辑
//     public function permission_edit(){
//         $role_id=$_GET['id'];
//         //所有的子权限
//         $permission_list = db('role_permission')->alias('a')->join('permission b','a.permission_id= b.id')->field('a.permission_id,b.name')->select();
//         $role_list =  db('role')->alias('a')->join('role_permission b','a.id= b.role_id')->join('permission c','b.permission_id= c.id')->field('a.*,b.permission_id,c.name as permission_name')->select();
//        dump($permission_list);die;
//         $this->assign('permission_list',$permission_list);
//         $this->assign('role_list',$role_list);
//         $this->assign('role_id',$role_id);
//         return view();

//     }

}
